home *** CD-ROM | disk | FTP | other *** search
- package icontrols.DataList;
-
- import com.ms.ado.AdoException;
- import com.ms.ado.DataSource;
- import com.ms.ado.Field;
- import com.ms.ado.Recordset;
- import com.ms.ado.RecordsetEvent;
- import com.ms.wd.app.Message;
- import com.ms.wd.core.CancelEvent;
- import com.ms.wd.core.Closure;
- import com.ms.wd.core.Event;
- import com.ms.wd.core.EventHandler;
- import com.ms.wd.core.Sys;
- import com.ms.wd.ui.Color;
- import com.ms.wd.ui.Control;
- import com.ms.wd.ui.Font;
- import com.ms.wd.ui.Graphics;
- import com.ms.wd.ui.MouseEvent;
- import com.ms.wd.ui.PaintEvent;
- import com.ms.wd.ui.Pen;
- import com.ms.wd.ui.Rectangle;
- import com.ms.wd.ui.ScrollBar;
- import com.ms.wd.ui.ScrollEvent;
- import com.ms.wd.ui.ScrollEventHandler;
- import com.ms.wd.ui.VScrollBar;
- import com.ms.wd.util.Debug;
- import com.ms.wd.util.Utils;
- import com.ms.wd.win32.RECT;
- import com.ms.wd.win32.TEXTMETRIC;
- import com.ms.wd.win32.Windows;
- import icontrols.BorderDisplayer;
- import icontrols.StringDisplayer;
- import icontrols.Data.DataProvider;
- import icontrols.Data.DataRow;
- import icontrols.Data.IDataConsumer;
- import icontrols.Data.RecordsetProvider;
-
- public class DataList extends Control implements IDataConsumer {
- private EventHandler valueChanged = null;
- private DataProvider dataProvider = null;
- private Recordset lookupList = null;
- private String boundColumn = null;
- private String listColumn = null;
- private ScrollBar scrollbar = new VScrollBar();
- private Rectangle clientRect = null;
- private Rectangle clientInsideRect = null;
- private BorderDisplayer borderDisplayer = new BorderDisplayer();
- private StringDisplayer stringDisplayer = new StringDisplayer();
- private StringDisplayer stringSelectedDisplayer = new StringDisplayer();
- private int borderStyle = 2;
- private Field boundField = null;
- private Field listField = null;
- private Field lookupListField = null;
- private int rowHeight = -1;
- private DataRow selectedRow = null;
- private DataRow hotTrack = null;
- private boolean trackSelection = false;
- private RecordsetProvider recordsetProvider = new RecordsetProvider();
- // $FF: synthetic field
- private static Class class$icontrols$DataList$BorderStyle;
-
- public void setListColumn(String listColumn) {
- if (!Utils.stringsEqual(listColumn, this.listColumn)) {
- this.listColumn = listColumn;
- this.bindToDisplayFields();
- ((Control)this).invalidate();
- }
-
- }
-
- public void onWillChangeFieldHandler(Object sender, RecordsetEvent event) {
- }
-
- public void onWillChangeRecordHandler(Object sender, RecordsetEvent event) {
- }
-
- public void onMouseMove(MouseEvent me) {
- if (this.dataProvider != null && this.listField != null && this.boundField != null) {
- int displayIndex = this.getDisplayIndexFromY(me.y);
- if (displayIndex >= 0) {
- if (!this.trackSelection) {
- this.setHotTrackRow(this.dataProvider.getDisplayRow(displayIndex));
- } else {
- this.setHotTrackRow((DataRow)null);
- this.setSelectedRow(this.dataProvider.getDisplayRow(displayIndex));
- }
- }
-
- super.onMouseMove(me);
- }
- }
-
- public DataSource getRowSource() {
- return this.recordsetProvider.getDataSource();
- }
-
- protected boolean isHandleOwnedByList(int hwnd) {
- return hwnd == ((Control)this).getHandle() || hwnd == this.scrollbar.getHandle();
- }
-
- public void setRowSource(DataSource dataSource) {
- this.recordsetProvider.setDataSource(dataSource);
- }
-
- private Rectangle getRowBounds(int displayIndex) {
- int height = this.getRowHeight();
- Rectangle inside = this.getCachedClientInsideRect();
- return new Rectangle(inside.x, inside.y + height * displayIndex, inside.width, height);
- }
-
- private int getDisplayIndexFromY(int y) {
- Rectangle inside = this.getCachedClientInsideRect();
- int displayIndex = -1;
- if (y > inside.y && y < inside.y + inside.height) {
- y -= inside.y;
- displayIndex = y / this.getRowHeight();
- }
-
- return displayIndex;
- }
-
- public void acquireRecordset(Object sender, Event event) {
- this.dataProvider = new DataProvider(this.recordsetProvider.getRecordset());
- this.dataProvider.setDataConsumer(this);
- this.lookupList = (Recordset)this.recordsetProvider.getRecordset().clone();
- this.bindToDisplayFields();
- this.doLayout();
- ((Control)this).invalidate();
- }
-
- public void releaseRecordset(Object sender, Event event) {
- if (this.dataProvider != null) {
- this.dataProvider.setDataConsumer((IDataConsumer)null);
- this.dataProvider = null;
- this.lookupList = null;
- this.boundField = null;
- this.listField = null;
- this.lookupListField = null;
- }
-
- }
-
- private Rectangle getCachedClientInsideRect() {
- if (this.clientRect == null) {
- Rectangle junk = this.getCachedClientRect();
- }
-
- Debug.assert(this.clientInsideRect != null, "Null client inside rect");
- return this.clientInsideRect;
- }
-
- public String getDisplayData(int displayIndex) {
- Debug.assert(this.dataProvider != null, "null dataprovider");
- Debug.assert(this.lookupList != null, "null lookuplist");
- Debug.assert(this.boundField != null, "null bound field");
- Debug.assert(this.listField != null, "null list field");
- Debug.assert(this.lookupListField != null, "null lookup list field");
- Debug.assert(Utils.stringsEqual(this.boundField.getName(), this.boundColumn) && this.boundColumn != null, "invalid bound field info");
- DataRow row = this.dataProvider.getDisplayRow(displayIndex);
- String rawData = this.dataProvider.getFormattedData(row, this.boundField);
- this.lookupList.setFilter(this.boundColumn + " = " + rawData);
- this.lookupList.moveFirst();
- return this.lookupListField.getString();
- }
-
- public String getDisplayData(String value) {
- Debug.assert(this.dataProvider != null, "null dataprovider");
- Debug.assert(this.lookupList != null, "null lookuplist");
- Debug.assert(this.boundField != null, "null bound field");
- Debug.assert(this.listField != null, "null list field");
- Debug.assert(this.lookupListField != null, "null lookup list field");
- Debug.assert(Utils.stringsEqual(this.boundField.getName(), this.boundColumn) && this.boundColumn != null, "invalid bound field info");
- this.lookupList.setFilter(this.boundColumn + " = " + value);
- this.lookupList.moveFirst();
- return this.lookupListField.getString();
- }
-
- public void onMouseLeave(MouseEvent me) {
- this.setHotTrackRow((DataRow)null);
- super.onMouseLeave(me);
- }
-
- public void onFieldChangeCompleteHandler(Object sender, RecordsetEvent event) {
- }
-
- public void onRecordChangeCompleteHandler(Object sender, RecordsetEvent event) {
- }
-
- public void addValueChangedHandler(EventHandler handler) {
- this.valueChanged = (EventHandler)Closure.combine(this.valueChanged, handler);
- }
-
- public String getBoundColumn() {
- return this.boundColumn;
- }
-
- public void setAsyncPostMessage(int message) {
- }
-
- public void onMoveCompleteHandler(Object sender, RecordsetEvent event) {
- DataRow old = this.selectedRow;
- this.selectedRow = this.dataProvider.getCurrentRow();
- this.dataProvider.scrollCurrentIntoView();
- this.invalidateRow(old);
- this.invalidateRow(this.selectedRow);
- if (!this.trackSelection) {
- this.onValueChanged(Event.EMPTY);
- }
-
- }
-
- public void onRecordsetChangeCompleteHandler(Object sender, RecordsetEvent event) {
- }
-
- private int computePageSize() {
- int height = this.getRowHeight();
- return (this.getCachedClientInsideRect().height + 2 * height - 1) / height;
- }
-
- private void bindToDisplayFields() {
- if (this.boundColumn != null && this.dataProvider != null) {
- this.boundField = this.dataProvider.getReadField(this.boundColumn);
- } else {
- this.boundField = null;
- }
-
- if (this.listColumn != null && this.dataProvider != null) {
- this.listField = this.dataProvider.getReadField(this.listColumn);
- } else {
- this.listField = null;
- }
-
- if (this.listField == null && this.boundField != null) {
- this.listField = this.boundField;
- }
-
- if (this.boundField == null && this.listField != null) {
- this.boundField = this.listField;
- }
-
- if (this.listField != null) {
- this.lookupListField = this.lookupList.getFields().getItem(this.listField.getName());
- }
-
- }
-
- private void doLayout() {
- this.clientRect = null;
- Rectangle inside = this.getCachedClientInsideRect();
- Rectangle bounds = this.scrollbar.getBounds();
- bounds.x = inside.x + inside.width;
- bounds.y = inside.y;
- bounds.height = inside.height;
- this.scrollbar.setBounds(bounds);
- this.scrollbar.setEnabled(this.dataProvider != null);
- if (this.dataProvider != null) {
- this.dataProvider.setPageSize(this.computePageSize());
- }
-
- this.updateVertScrollbar();
- }
-
- public int getAsyncPostHwnd() {
- return 0;
- }
-
- public void setBoundColumn(String boundColumn) {
- if (!Utils.stringsEqual(boundColumn, this.boundColumn)) {
- this.boundColumn = boundColumn;
- this.bindToDisplayFields();
- this.doLayout();
- ((Control)this).invalidate();
- }
-
- }
-
- protected void onResize(Event e) {
- if (this.clientRect != null) {
- int borderWidth = this.borderDisplayer.getBorderWidth();
- int borderHeight = this.borderDisplayer.getBorderHeight();
- Rectangle right = null;
- Rectangle bottom = null;
- right = new Rectangle(this.clientRect.x + this.clientRect.width - borderWidth, this.clientRect.y, borderWidth, this.clientRect.height);
- bottom = new Rectangle(this.clientRect.x, this.clientRect.y + this.clientRect.height - borderHeight, this.clientRect.width, borderHeight);
- Rectangle newClientRect = ((Control)this).getClientRect();
- if (newClientRect.width != this.clientRect.width) {
- ((Control)this).invalidateRect(right);
- right = new Rectangle(newClientRect.x + newClientRect.width - borderWidth, newClientRect.y, borderWidth, newClientRect.height);
- ((Control)this).invalidateRect(right);
- }
-
- if (newClientRect.height != this.clientRect.height) {
- ((Control)this).invalidateRect(bottom);
- bottom = new Rectangle(newClientRect.x, newClientRect.y + newClientRect.height - borderHeight, newClientRect.width, borderHeight);
- ((Control)this).invalidateRect(bottom);
- }
- }
-
- super.onResize(e);
- this.doLayout();
- }
-
- private void updateVertScrollbar() {
- if (this.dataProvider != null) {
- int pageSize = this.dataProvider.getPageSize();
- int totalSize = this.dataProvider.getTotalRowCount();
- float currentRatio = this.dataProvider.getTopRowRatio();
- if (totalSize > pageSize) {
- this.scrollbar.setValues(0, totalSize, 1, pageSize, (int)(currentRatio * (float)totalSize));
- } else {
- this.scrollbar.setValues(0, 1, 1, 1, 0);
- }
- }
-
- }
-
- private int getRowHeight() {
- if (this.rowHeight < 0) {
- this.setRowHeight(this.computeRowHeight());
- }
-
- return this.rowHeight;
- }
-
- private void setRowHeight(int rowHeight) {
- if (rowHeight != this.rowHeight) {
- this.rowHeight = rowHeight;
- this.doLayout();
- ((Control)this).invalidate();
- }
-
- }
-
- protected void setTrackSelection(boolean selecting) {
- this.trackSelection = selecting;
- }
-
- private void setSelectedRow(DataRow row) {
- if (row != null) {
- if (!DataProvider.rowsEqual(row, this.selectedRow)) {
- this.dataProvider.setCurrentRow(row.getBookmark());
- this.dataProvider.scrollCurrentIntoView();
- DataRow old = this.selectedRow;
- this.selectedRow = this.dataProvider.getCurrentRow();
- this.invalidateRow(old);
- this.invalidateRow(this.selectedRow);
- }
- } else if (this.selectedRow != null) {
- DataRow old = this.selectedRow;
- this.selectedRow = null;
- this.invalidateRow(old);
- if (!this.trackSelection) {
- this.onValueChanged(Event.EMPTY);
- }
- }
-
- }
-
- public String getValue() {
- String ret = null;
- if (this.selectedRow != null && this.dataProvider != null && this.boundField != null) {
- ret = this.dataProvider.getFormattedData(this.selectedRow, this.boundField);
- }
-
- return ret;
- }
-
- protected void propertyChanged(int property) {
- switch (property) {
- case 6:
- this.setRowHeight(this.computeRowHeight());
- default:
- super.propertyChanged(property);
- }
- }
-
- public void setValue(String value) {
- if (!Utils.stringsEqual(this.getValue(), value)) {
- if (value == null) {
- this.setSelectedRow((DataRow)null);
- } else {
- Debug.assert(this.dataProvider != null, "null dataprovider");
- Debug.assert(this.lookupList != null, "null lookuplist");
- Debug.assert(this.boundField != null, "null bound field");
- Debug.assert(Utils.stringsEqual(this.boundField.getName(), this.boundColumn) && this.boundColumn != null, "invalid bound field info");
- this.lookupList.setFilter(this.boundColumn + " = " + value);
- Object bookmark = null;
-
- try {
- this.lookupList.moveFirst();
- bookmark = this.lookupList.getBookmark();
- } catch (Exception var4) {
- bookmark = null;
- }
-
- if (bookmark != null) {
- this.dataProvider.setCurrentRow(bookmark);
- this.dataProvider.scrollCurrentIntoView();
- DataRow old = this.selectedRow;
- this.selectedRow = this.dataProvider.getCurrentRow();
- this.invalidateRow(old);
- this.invalidateRow(this.selectedRow);
- }
- }
- }
-
- }
-
- public DataList() {
- ((Control)this).setBackColor(Color.WINDOW);
- this.recordsetProvider.setOnAcquireRecordset(new EventHandler(this, "acquireRecordset"));
- this.recordsetProvider.setOnReleaseRecordset(new EventHandler(this, "releaseRecordset"));
- this.stringDisplayer.setFont((Font)null);
- this.stringDisplayer.setBackColor((Color)null);
- this.stringDisplayer.setForeColor((Color)null);
- this.stringDisplayer.setAlignment(5);
- this.stringSelectedDisplayer.setBackColor(Color.HIGHLIGHT);
- this.stringSelectedDisplayer.setForeColor(Color.HIGHLIGHTTEXT);
- this.stringSelectedDisplayer.setAlignment(5);
- this.setupBorder();
- ((Control)this).add(this.scrollbar);
- this.scrollbar.setVisible(true);
- this.scrollbar.addOnScroll(new ScrollEventHandler(this, "onVertScroll"));
- ((Control)this).setBounds(0, 0, 121, 121);
- this.doLayout();
- }
-
- public void onViewScrolled(boolean offsetValid, int offset) {
- ((Control)this).invalidate();
- }
-
- public void onMouseUp(MouseEvent me) {
- if (this.dataProvider != null && this.listField != null && this.boundField != null) {
- this.trackSelection = false;
- ((Control)this).setCapture(false);
- this.onValueChanged(Event.EMPTY);
- super.onMouseUp(me);
- }
- }
-
- public void onEndOfRecordsetHandler(Object sender, RecordsetEvent event) {
- }
-
- public void onWillChangeRecordsetHandler(Object sender, RecordsetEvent event) {
- }
-
- public void onWillMoveHandler(Object sender, RecordsetEvent event) {
- }
-
- public String getRowMember() {
- return this.recordsetProvider.getDataMember();
- }
-
- public void removeValueChangedHandler(EventHandler handler) {
- this.valueChanged = (EventHandler)Closure.remove(this.valueChanged, handler);
- }
-
- public void setRowMember(String dataMember) {
- this.recordsetProvider.setDataMember(dataMember);
- }
-
- protected void wndProc(Message m) {
- boolean callDefault = true;
- switch (m.msg) {
- case 20:
- callDefault = false;
- break;
- case 135:
- callDefault = false;
- m.result = 0;
- m.result |= 1;
- }
-
- if (callDefault) {
- super.wndProc(m);
- }
-
- }
-
- protected void onValueChanged(Event event) {
- if (this.valueChanged != null) {
- this.valueChanged.invoke(this, event);
- }
-
- }
-
- public int getBorderStyle() {
- return this.borderStyle;
- }
-
- public void setBorderStyle(int borderStyle) {
- if (this.borderStyle != borderStyle) {
- if (borderStyle < 0 || borderStyle > 3) {
- throw new IllegalArgumentException(Sys.getString("InvalidEnumArgument", "borderStyle", (new Integer(borderStyle)).toString(), (class$icontrols$DataList$BorderStyle != null ? class$icontrols$DataList$BorderStyle : (class$icontrols$DataList$BorderStyle = class$("icontrols.DataList.BorderStyle"))).getName()));
- }
-
- this.borderStyle = borderStyle;
- this.setupBorder();
- }
-
- }
-
- // $FF: synthetic method
- private static Class class$(String s) {
- try {
- return Class.forName(s);
- } catch (ClassNotFoundException e) {
- throw new NoClassDefFoundError(((Throwable)e).getMessage());
- }
- }
-
- public void onWillViewScroll(Object sender, CancelEvent cancel) {
- }
-
- public void onVertScroll(Object sender, ScrollEvent se) {
- se.useNewPos = false;
- if (((Control)this).getEnabled()) {
- boolean updateScrollbar = true;
- DataRow current = this.dataProvider.getDisplayRow(0);
- switch (se.type) {
- case 0:
- if (current != null) {
- this.dataProvider.scroll(-1, current.getBookmark());
- } else {
- this.dataProvider.scrollToFirst();
- }
- break;
- case 1:
- if (current != null) {
- this.dataProvider.scroll(1, current.getBookmark());
- } else {
- this.dataProvider.scrollToBottom();
- }
- break;
- case 2:
- if (current != null) {
- this.dataProvider.scroll(-1 * this.dataProvider.getPageSize(), current.getBookmark());
- } else {
- this.dataProvider.scrollToFirst();
- }
- break;
- case 3:
- if (current != null) {
- this.dataProvider.scroll(this.dataProvider.getPageSize(), current.getBookmark());
- } else {
- this.dataProvider.scrollToBottom();
- }
- break;
- case 4:
- int totalSize = this.dataProvider.getTotalRowCount();
- this.dataProvider.scroll((float)se.newPos / (float)totalSize);
- break;
- case 5:
- updateScrollbar = false;
- }
-
- if (updateScrollbar) {
- this.updateVertScrollbar();
- }
-
- }
- }
-
- private Rectangle getCachedClientRect() {
- if (this.clientRect == null) {
- int borderWidth = this.borderDisplayer.getBorderWidth();
- int borderHeight = this.borderDisplayer.getBorderHeight();
- Rectangle client = ((Control)this).getClientRect();
- Rectangle inside = new Rectangle();
- inside.x = client.x + borderWidth;
- inside.y = client.y + borderHeight;
- inside.width = client.width - 2 * borderWidth - this.scrollbar.getBounds().width;
- inside.height = client.height - 2 * borderHeight;
- this.clientInsideRect = inside;
- this.clientRect = client;
- }
-
- Debug.assert(this.clientRect != null, "Null client rect");
- return this.clientRect;
- }
-
- private int computeRowHeight() {
- Graphics g = ((Control)this).createGraphics();
- g.setFont(((Control)this).getFont());
- TEXTMETRIC tm = new TEXTMETRIC();
- Windows.GetTextMetrics(g.getHandle(), tm);
- int height = tm.tmHeight + 4;
- g.dispose();
- return height;
- }
-
- private void setupBorder() {
- switch (this.borderStyle) {
- case 0:
- this.borderDisplayer.setBorderStyle(4);
- break;
- case 1:
- this.borderDisplayer.setBorderStyle(1);
- break;
- case 2:
- this.borderDisplayer.setBorderStyle(18);
- break;
- case 3:
- this.borderDisplayer.setBorderStyle(34);
- }
-
- this.doLayout();
- ((Control)this).invalidate();
- }
-
- public void onMouseDown(MouseEvent me) {
- if (this.dataProvider != null && this.listField != null && this.boundField != null) {
- this.trackSelection = true;
- int displayIndex = this.getDisplayIndexFromY(me.y);
- if (displayIndex >= 0) {
- this.setSelectedRow(this.dataProvider.getDisplayRow(displayIndex));
- }
-
- ((Control)this).setCapture(true);
- super.onMouseDown(me);
- }
- }
-
- private void invalidateRow(DataRow row) {
- if (row != null) {
- DataRow rowLookup = this.dataProvider.getDisplayRow(row.getBookmark());
- if (rowLookup != null) {
- this.invalidateRow(rowLookup.getDisplayIndex());
- }
-
- }
- }
-
- private void invalidateRow(int displayIndex) {
- if (displayIndex >= 0) {
- Rectangle rect = this.getRowBounds(displayIndex);
- if (rect != null) {
- ((Control)this).invalidateRect(rect);
- ((Control)this).update();
- }
-
- }
- }
-
- protected String selectByMatch(String value) {
- if (this.dataProvider != null && this.lookupList != null && this.boundField != null && this.listField != null && this.lookupListField != null) {
- this.lookupList.setFilter(this.listColumn + " LIKE '" + value + "%'");
-
- try {
- this.lookupList.moveFirst();
- } catch (AdoException var3) {
- }
-
- if (!this.lookupList.getEOF() && !this.lookupList.getBOF()) {
- this.dataProvider.setCurrentRow(this.lookupList.getBookmark());
- return this.lookupListField.getString();
- }
- }
-
- return null;
- }
-
- private void setHotTrackRow(DataRow row) {
- if (row != null) {
- if (!DataProvider.rowsEqual(row, this.hotTrack)) {
- DataRow old = this.hotTrack;
- this.hotTrack = row;
- this.invalidateRow(old);
- this.invalidateRow(this.hotTrack);
- }
- } else if (this.hotTrack != null) {
- DataRow old = this.hotTrack;
- this.hotTrack = null;
- this.invalidateRow(old);
- }
-
- }
-
- private void onNoRowsetPaint(PaintEvent pe) {
- this.borderDisplayer.paintIn(pe.graphics, this.getCachedClientRect(), pe.clipRect);
- pe.graphics.setPen((Pen)null);
- pe.graphics.drawRect(this.getCachedClientInsideRect());
- }
-
- protected void onPaint(PaintEvent pe) {
- if (this.dataProvider != null && this.listField != null && this.boundField != null) {
- Rectangle inside = this.getCachedClientInsideRect();
- Rectangle client = this.getCachedClientRect();
- this.borderDisplayer.paintIn(pe.graphics, client, pe.clipRect);
- pe.graphics.intersectClipRect(inside);
- int pageRowCount = this.dataProvider.getPageRowCount();
- int lastY = 0;
- RECT testRect = new RECT();
- int dc = pe.graphics.getHandle();
- testRect.left = inside.x;
- testRect.right = inside.x + inside.width;
-
- for(int displayIndex = 0; displayIndex < pageRowCount; ++displayIndex) {
- Rectangle rowBounds = this.getRowBounds(displayIndex);
- testRect.top = rowBounds.y;
- testRect.bottom = rowBounds.y + rowBounds.height;
- if (Windows.RectVisible(dc, testRect)) {
- StringDisplayer displayer = this.stringDisplayer;
- if (DataProvider.rowsEqual(this.selectedRow, this.dataProvider.getDisplayRow(displayIndex))) {
- displayer = this.stringSelectedDisplayer;
- } else if (DataProvider.rowsEqual(this.hotTrack, this.dataProvider.getDisplayRow(displayIndex))) {
- this.stringDisplayer.setForeColor(Color.HOTTRACK);
- }
-
- displayer.paintIn(pe.graphics, rowBounds, pe.clipRect, this.getDisplayData(displayIndex));
- this.stringDisplayer.setForeColor((Color)null);
- }
-
- lastY = rowBounds.y + rowBounds.height;
- }
-
- if (lastY < inside.y + inside.height) {
- pe.graphics.setPen((Pen)null);
- pe.graphics.drawRect(inside.x, lastY, inside.width, inside.height - lastY + this.borderDisplayer.getBorderHeight());
- }
-
- } else {
- this.onNoRowsetPaint(pe);
- }
- }
-
- public String getListColumn() {
- return this.listColumn;
- }
- }
-